/*
 * @(#)Nodes.java 1.50 04/04/14
 * 
 * Clase Compartida
 * PROP Grupo 13 Cluster 3
 */

/**
 * Esta clase implementa un ArrayList de node usable como una lista ordenada de de Node
 * 
 * @author David Molins Gom�
 */


package dominio;

import java.util.ArrayList;

//import test.Testing;


public class Nodes extends GraphElement{
	public ArrayList<Node> nodelist;
	public int coste_total; // NO SE SI HAURIA DE SER-HI AQU�
	public ArrayList<Cost> costs; //


	//Creadoras
	public Nodes (int _id, String _name, String _description) {
        super(_id, _name, _description);
		this.id = _id;
		this.name = _name;
		this.description = _description;
		this.coste_total = 0;//CalculateCosteTotal();
		this.nodelist = new ArrayList<Node>();
		this.costs = new ArrayList<Cost>();
	}


    public Nodes (int _id, String _name, String _description, ArrayList<Node> _nodes) {
        super(_id, _name, _description);
        this.id = _id;
        this.name = _name;
        this.description = _description;
        this.nodelist = _nodes;
        this.coste_total = CalculateCosteTotal(); //funciona si los nodos dentro del nodelist tienen un tiempo;
        this.costs = new ArrayList<Cost>();
    }

    public Nodes (int _id, String _name, String _description, ArrayList<Node> _nodes, ArrayList<Cost> _costs) {
        super(_id, _name, _description, _costs);
        this.id = _id;
        this.name = _name;
        this.description = _description;
        this.nodelist = _nodes;
        this.coste_total = CalculateCosteTotal();
        this.costs = _costs;
    }

    public Nodes (int id){
        super(id);
		this.id = id;
	}

	public Nodes (){}

	//Setters
	public void SetName(String name){
		this.name = name;
	}

	public void SetDescription(String description){
		this.description = description;
	}
	
	public void SetCosteTotal(Integer CosteTotal){
		this.coste_total = CosteTotal;
	}
	
	public void SetCosteTotal(){
		this.coste_total = CalculateCosteTotal();
	}

	public void SetNodelist(ArrayList<Node> nodelist){
		this.nodelist = nodelist;
	}

	public void SetCosts(ArrayList<Cost> costs){
        SetGraphElementCostsList(costs);
		this.costs = costs;
	}

	public Node GetNode(Integer index){
		return nodelist.get(index);   //retorna el elemento id-esimo del nodelist;
	}

	public void AddNode(Node nodo){ // anade nodo al final de nodelist
		nodelist.add(nodo);
	}

	public void InsertNodeEn(Node nodo){ //anade el nodo al final de nodelist
		nodelist.add(nodo.GetId(), nodo);
	}
	
	public Integer CalculateCosteTotal(){ // calcula el coste total de realizar las tareas en s� en orden de nodelist
		Integer reccount = 0;
		if(! this.nodelist.isEmpty()){
			for(Integer i = 0; i < nodelist.size(); i++){
				reccount += this.nodelist.get(i).GetTime();
			}
		}
		return reccount;
	}
	
	
	public Integer CalculateTime(){   // retorna el coste en tiempo del recorrido por los nodos en orden de nodelist, del primero hasta el del final
		Integer time = 0;
		Node first = nodelist.get(0);
		Node second;
		for( int i = 1; i < nodelist.size(); ++i){
			second = nodelist.get(i);
			time += first.GetCostTo(second.GetId());
		}

		return time;
	}

	public Integer CalculateTime(Integer n){	//Calcula el coste en tiempo del camino hasta el n-esimo node de la nodelist. No a�ade el tiempo de node solo el de los caminos
		Integer time = 0;
		Node first = nodelist.get(0);
		Node second;
		for( int i = 1; i < n; ++i){
			second = nodelist.get(i);
			time += first.GetCostTo(second.GetId());
		}

		return time;
	}

	//Getters
	public Integer GetSize(){ // devuelve tama�o de nodelist
		return nodelist.size();
	}

	public String GetName(){
		return this.name;
	}

	public String GetDescription(){
		return this.description;
	}

	public ArrayList<Node> GetNodelist(){
		return this.nodelist;
	}

	public ArrayList<Cost> GetCosts(){
		return this.costs;
	}

	public Integer GetCosteTotal(){
		return this.coste_total;
	}

	public void PrintNodes(){  // imprime el parametro implicito
		System.out.println("[Nodes]id: "+this.id+"");
		System.out.println("[Nodes]name: "+this.name+"");
		System.out.println("[Nodes]description: "+this.description+"");
		System.out.println("[Nodes]coste_total: "+this.coste_total+"");
		for(Integer i = 0; i < costs.size(); i++){
			Cost c =costs.get(i);
			c.PrintCost();//System.out.println("[id:"+c.GetId()+"]="+c.GetCost()+" ");
		}
		System.out.println("\n");
		System.out.println("[Nodes]nodelist: ");
		if (!nodelist.equals(null)) { //JG
			for(Integer i = 0; i < nodelist.size(); i++){
				nodelist.get(i).PrintNode();
			}
		}
		System.out.println("\n");
	}
}